package com.jinsilu.freight.ui.fragment;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jinsilu.freight.R;
import com.jinsilu.freight.common.MyFragment;
import com.jinsilu.freight.ui.activity.HomeActivity;

public class WaybillFragment extends MyFragment<HomeActivity> {

    private TabLayout mTabLayout;
    private ViewPager2 mViewPager;

    private final int activeColor = Color.parseColor("#ffffff");
    private final int normalColor = Color.parseColor("#ffffff");

    private final int activeSize = 16;
    private final int normalSize = 16;

    private TabLayoutMediator mediator;

    private final String[] tabs = new String[]{"未完成", "已完成"};

    public static WaybillFragment newInstance() {
        return new WaybillFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.waybill_fragment;
    }

    @Override
    protected void initView() {
        mTabLayout = findViewById(R.id.tab_layout);
        mViewPager = findViewById(R.id.view_pager);

        //禁用预加载
        mViewPager.setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT);
        //Adapter
        mViewPager.setAdapter(new FragmentStateAdapter(getChildFragmentManager(), getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                //FragmentStateAdapter内部自己会管理已实例化的fragment对象。
                // 所以不需要考虑复用的问题
                if (0 == position)
                    return UnfinishedWaybillFragment.newInstance();
                else
                    return FinishedWaybillFragment.newInstance();
            }

            @Override
            public int getItemCount() {
                return tabs.length;
            }
        });
        //viewPager 页面切换监听
        mViewPager.registerOnPageChangeCallback(changeCallback);

        mediator = new TabLayoutMediator(mTabLayout, mViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //这里可以自定义TabView
                TextView tabView = new TextView(getContext());

                int[][] states = new int[2][];
                states[0] = new int[]{android.R.attr.state_selected};
                states[1] = new int[]{};

                int[] colors = new int[]{activeColor, normalColor};
                ColorStateList colorStateList = new ColorStateList(states, colors);
                tabView.setText(tabs[position]);
                tabView.setTextSize(normalSize);
                tabView.setTextColor(colorStateList);

                tab.setCustomView(tabView);
            }
        });
        //要执行这一句才是真正将两者绑定起来
        mediator.attach();
    }

    @Override
    protected void initData() {

    }

    private final ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            //可以来设置选中时tab的大小
            int tabCount = mTabLayout.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                TabLayout.Tab tab = mTabLayout.getTabAt(i);
                TextView tabView = null;
                if (tab != null) {
                    tabView = (TextView) tab.getCustomView();
                }
                if (tab != null) {
                    if (tab.getPosition() == position) {
                        if (tabView != null) {
                            tabView.setTextSize(activeSize);
                            tabView.setTypeface(Typeface.DEFAULT_BOLD);
                        }
                    } else {
                        if (tabView != null) {
                            tabView.setTextSize(normalSize);
                            tabView.setTypeface(Typeface.DEFAULT);
                        }
                    }
                }
            }
        }
    };

    /**
     * 跳转至未完成运单页
     *
     * @param index 未完成运单页索引
     */
    public void setWaybillCurrentPage(int index) {
        if (null != mViewPager) mViewPager.setCurrentItem(index);
    }

    @Override
    public void onDestroyView() {
        if (null != mediator)
            mediator.detach();
        if (null != mViewPager && null != changeCallback)
            mViewPager.unregisterOnPageChangeCallback(changeCallback);
        super.onDestroyView();
    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }
}
